﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RafikiSIS_Site.Models;
using System.Data;

namespace MVC.Controllers
{
    public class EntityEditController : Controller
    {
        // GET: EntityEdit
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult EditEntity()
        {
            List<ViewEntityAttributes> EntityAttrsList = new List<ViewEntityAttributes>();
            ViewBag.AttList = EntityAttrsList;

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Users = "tchase,jstocksdale")]
        public ActionResult EditEntity(string commandctrl,string EntityID)
        {
            DataTable EntityInfo = DataFuncs.GetAnyTable("Entities", "EntityID = " + EntityID);
            if (EntityInfo.Rows.Count != 1)
            {
                ViewBag.Feedback = "There is no entity with that ID";
                ViewBag.Error = true;
                return View();
            } 
            else
            {
                ViewBag.Error = false;
            }

            List<ViewEntityAttributes> EntityAttrsList = new List<ViewEntityAttributes>();
            Dictionary<string, string> myentity = new Dictionary<string, string>();
            myentity.Add("RequestorEntityID", "0");
            // Specify the EntityType and EntitySubType in order to display all data items.
            myentity.Add("RequestedChain", EntityInfo.Rows[0]["EntityType"].ToString() + (EntityInfo.Rows[0]["EntitySubType"] ?? "").ToString());
            myentity.Add("CommandType", "Attributes");
            DataTable Attributes = DataFuncs.GetSQLsp("ReadDataFromDB_New", myentity);
            foreach (DataRow row in Attributes.Rows)
            {
                EntityAttrsList.Add(new ViewEntityAttributes() { AttributeName = row["AttributeName"].ToString(), 
                                                                 AttributeType = row["DataType"].ToString(), 
                                                                 Editable = Convert.ToBoolean(row["Editable"]) });
            }
            ViewBag.AttList = EntityAttrsList;
            ViewBag.EntityID = EntityID;
            ViewBag.EntityTypeSubType = EntityInfo.Rows[0]["EntityType"].ToString() + (EntityInfo.Rows[0]["EntitySubType"] ?? "").ToString();

            return View();
        }
        public ActionResult CreateEntity()
        {
            return View();
        }
        public ActionResult EditRelationship()
        {
            return View();
        }
        public ActionResult CreateRelationship()
        {
            return View();
        }
    
    }
}